package com.jhws.business.advertisement.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.advertisement.bo.NoticeInfo;
import com.jhws.business.advertisement.bo.AdvConstant;
import com.jhws.business.advertisement.bo.AdvJson;
import com.jhws.business.advertisement.bo.NoticeUpdateData;
import com.jhws.business.advertisement.bo.TblNoticeDimBo;
import com.jhws.business.advertisement.service.ITblNoticeDimService;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.sys.menu.service.ITblMenuDimService;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.AccountsUtils;
import com.jhws.util.DateUtil;
import com.jhws.util.GetId;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/NoticeCtrl")
public class NoticeCtrl {
    @Resource
    protected ITblNoticeDimService ITblNoticeDimService;
    @Resource
    protected ITblMenuDimService ITblMenuDimService;
    @Resource
    protected ITblCommDimService ITblCommDimService;
    @Resource
    protected ITblBuildingDimService ITblBuildingDimService;
    @Resource
    protected ITblUserDimService IUserService;
    @Resource
    private ITblOwnerDimService ownerDimService;
    @Resource
    private ITblCommDimService commDimService;
    @Resource
    private DataUpdateService dataUpdateService;

    @RequestMapping("query.x")
    public String queryTblNoticeDim() throws Exception {
        return "business/noticeDim/queryNotice";
    }

    @RequestMapping("queryNoticePage.x")
    @ResponseBody
    public Object queryNoticeListPage(@RequestParam Map map) throws Exception {
        return ITblNoticeDimService.findNoticeListPage(map);
    }

    @RequestMapping("forSave.x")
    public String forSaveTblNoticeDim() throws Exception {
        return "business/noticeDim/addNotice";
    }


    @RequestMapping("addNotice.x")
    @ResponseBody
    public Object addNotice(NoticeInfo noticeInfo) throws Exception {
        TblUserDimBo userInfo = IUserService.currentUser();
        boolean isAdmin = userInfo.getLogName() != null && userInfo.getLogName().equals("admin");
        AdvJson advJson = new AdvJson(null, noticeInfo.getTitle(), noticeInfo.getContent(),
                null, GetId.randomID(), "1", isAdmin);

        Long timeStamp = System.currentTimeMillis();
        if (noticeInfo.getType() == 0) {
            String[] commtags = noticeInfo.getComms().split(",");
            for (String label : commtags) {
                dataUpdateService.pushAndUpdate(timeStamp, NoticeUpdateData.NOTICE, label, false, true, noticeInfo.getTitle(), advJson);
            }
            saveTblNoticeDim(noticeInfo, commtags);
        } else if (noticeInfo.getType() == 2) {
            String[] commtags = noticeInfo.getComms().split(",");
            for (String label : commtags) {
                dataUpdateService.pushAndUpdate(timeStamp, NoticeUpdateData.NOTICE, label, false, true, noticeInfo.getTitle(), advJson);
            }
            saveTblNoticeDim(noticeInfo, commtags);
        } else {
            if (StringUtils.isEmpty(noticeInfo.getRoomNum())) {
                return new JsonRespData("", -1, "添加失败：房号不正确！");
            }
            if (noticeInfo.getRoomNum().length() == 12) {
                List<TblOwnerDimBo> mobiles = ownerDimService.findTblOwnerDimByWhere("where 1=1 and room_info_num = '" + noticeInfo.getRoomNum() + "'");
                if (mobiles.size() > 0) {
                    for (TblOwnerDimBo ownerDimBo : mobiles) {
                        dataUpdateService.pushAndUpdate(timeStamp, NoticeUpdateData.NOTICE, ownerDimBo.getOwnerMob(), true, true, noticeInfo.getTitle(), advJson);
                    }
                }
                dataUpdateService.pushAndUpdate(timeStamp, NoticeUpdateData.NOTICE, noticeInfo.getRoomNum(), true, true, noticeInfo.getTitle(), advJson);
                saveTblNoticeDim(noticeInfo, null);
            } else {
                return new JsonRespData("", -1, "添加失败：房号不正确！");
            }
        }
        return new JsonRespData("", 1, "添加成功");
    }

    private void saveTblNoticeDim(NoticeInfo noticeInfo, String[] commtags) throws Exception {
        TblNoticeDimBo bo = new TblNoticeDimBo();
        TblUserDimBo user = IUserService.currentUser();
        bo.setUserCode(user.getUserCode());
        bo.setCreateUsr(user.getLogName());
        bo.setTitile(noticeInfo.getTitle());
        bo.setContext(noticeInfo.getContent());
        if (StringUtils.isNotEmpty(noticeInfo.getStartTime())){
            bo.setBeginDate(DateUtil.stringDate(noticeInfo.getStartTime(), DateUtil.DATE_FORMAT));
        }
        if (StringUtils.isNotEmpty(noticeInfo.getEndTime())){
            bo.setEndDate(DateUtil.stringDate(noticeInfo.getEndTime(), DateUtil.DATE_FORMAT));
        }
        Date now = new Date();
        bo.setCreateTime(now);
        bo.setTimestamp(now.getTime());
        if (noticeInfo.getType() == 0) {
            bo.setNoticeType(AdvConstant.NOTICE_TYPE_COMM);
            for (String commCode : commtags) {
                bo.setNoticeCode(null);
                bo.setCommCode(commCode);
                bo.setEquType(commCode);
                ITblNoticeDimService.save(bo);
            }
        } else if (noticeInfo.getType() == 2) {
            bo.setNoticeType(AdvConstant.NOTICE_TYPE_MOBILE);
            for (String commCode : commtags) {
                bo.setNoticeCode(null);
                bo.setCommCode(commCode);
                bo.setEquType(commCode);
                ITblNoticeDimService.save(bo);
            }
        } else {
            bo.setNoticeType(AdvConstant.NOTICE_TYPE_ROOM);
            String commCode = noticeInfo.getRoomNum().substring(0, 4);
            bo.setCommCode(commCode);
            bo.setEquType(noticeInfo.getRoomNum());
            ITblNoticeDimService.save(bo);
        }
    }

    @RequestMapping("remove.x")
    @ResponseBody
    public Object removeTblNoticeDim(String noticeCode) throws Exception {
        ITblNoticeDimService.removeNoticeDim(noticeCode);
        return new JsonRespData("", 1, "删除成功");
    }

    @RequestMapping("view.x")
    public ModelAndView viewTblNoticeDim(String noticeCode) throws Exception {
        ModelAndView mv = new ModelAndView();
        TblNoticeDimBo bo = ITblNoticeDimService.findTblNoticeDimById(noticeCode);
        if (bo == null) {
            mv.setViewName("/open/webApiError.x?httpStatus=-1&errorInfo=该公告信息已删除！&content=该公告信息已删除！");
            return mv;
        }
        String putObject = "";
        String commStrs = bo.getEquType();
        if (AdvConstant.NOTICE_TYPE_COMM.equals(bo.getNoticeType())) {
            String[] commtags = commStrs.split(",");
            for (String comm : commtags) {
                TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(comm);
                putObject += "[" + commDimBo.getCommName() + "]";
            }
        } else if (AdvConstant.NOTICE_TYPE_ROOM.equals(bo.getNoticeType())) {
            String comm = commStrs.substring(0, 4);
            TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(comm);
            putObject += "[" + commDimBo.getCommName() + "]" + AccountsUtils.getBuildingInfo(commStrs);
        } else if (AdvConstant.NOTICE_TYPE_MOBILE.equals(bo.getNoticeType())) {
            putObject += "手机全量推送";
        }

        mv.addObject("noticeInfo", bo);
        mv.addObject("putObject", putObject);
        mv.setViewName("business/noticeDim/viewNotice");
        return mv;
    }

}
